package com.winning.sx.microframework.common.support;

import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.model.message.AjaxResponseMessage;
import net.sf.json.JSONObject;

import java.util.Map;

/**
 * @author wangj_envy
 * @date 2019年6月5日9:28:39
 */
public class Ret extends AjaxDataResponseMessage {

    public static Ret success (){
        Ret ret = new Ret();
        ret.setSuccess(true);
        return ret;
    }

    public static Ret success (Object data){
        Ret ret = new Ret();
        try {
            Map retData=(Map) data;
            if("0000".equals(retData.get("code").toString())){
                ret.setSuccess(true);
            }else{
                ret.setSuccess(false);
            }
            ret.setData(data);
        }catch (Exception e){
            ret.setData(data);
            ret.setSuccess(false);
        }
        return ret;
    }

    public static Ret error (String errorMsg){
        Ret ret = new Ret();
        ret.setSuccess(false);
        ret.setErrorMsg(errorMsg);
        return ret;
    }
    public static Ret error (String errorMsg,int errCode){
        Ret ret = new Ret();
        ret.setSuccess(false);
        ret.setErrorMsg(errorMsg);
        ret.setData(errCode);
        return ret;
    }

    @Override
    public String toString() {
        JSONObject jsonObject = JSONObject.fromObject(this);
        return jsonObject.toString();
    }
}
